#ifndef SERVER_H
#define SERVER_H
#include <map>
#include <list>
#include <set>
#include <sstream>
#include <string.h>
#include <stdint.h>
#include <pthread.h>

typedef struct sockaddr sockaddr_t;
typedef struct sockaddr_in sockaddr_in_t;

class Client {
public:
  Client(int sock);
  ~Client();

  void send(const void* data, uint32_t len);
  
  void setName(const std::string& name) {
    _name = name;
  }

  const std::string& name() const {
    return _name;
  }
private:
  static void* worker(Client* self);

  int _sock;
  pthread_t _worker;
  std::string _name;
};

class Server {
public:
  Server();
  ~Server();
  
  bool isOk() const;

  int mainLoop();
  void handle(Client* client, const char* msg);
  void remove(Client*);
private:
  void handleBroadcast(const char* msg, const sockaddr_t* addr);

  int svr_sock, bcast_sock;
  bool _initialized;

  std::list<Client*> _clients;
  std::list<std::string> _messages;

  std::set<std::string> _clientNames;
};

#endif
